/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.admin;

import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import javax.ejb.EJB;
import model.*;
import java.util.*;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

/**
 *
 * @author shinji
 */
@Named(value = "a_stateManagedBean")
@SessionScoped
public class StateManagedBean implements Serializable {
    @EJB
    private StateFacade stateFacade;

    private List<State> list = new ArrayList<State>();      // List of State belong Country
    private State obj = new State();                        // Object State
    private Country objC = new Country();                   // Object Country
    private String Name;                                    // Name of State

    public Country getObjC() {
        return objC;
    }

    public void setObjC(Country objC) {
        this.objC = objC;
    }

    public List<State> getList() {
        return list;
    }

    public void setList(List<State> list) {
        this.list = list;
    }

    public State getObj() {
        return obj;
    }

    public void setObj(State obj) {
        this.obj = obj;
    }

    public String getName() {
        return Name;
    }

    public void setName(String Name) {
        this.Name = Name;
    }
    
    /**
     * Creates a new instance of StateManagedBean
     */
    public StateManagedBean() {
    }
    
    /**
     * ********************************************
     */
    
    public void create() {
        try {
            obj = new State();
            obj.setState(Name);
            obj.setIDCountry(objC);

            this.stateFacade.create(obj);
            set_list();
            Name = "";

            set_message(FacesMessage.SEVERITY_INFO, "Create Religion", "Create " + obj.getState()+ " successfull.");
        } catch (Exception e) {
            set_message(FacesMessage.SEVERITY_WARN, "Create Religion", "Create " + obj.getState() + " error !!!");
        }
    }

    public void edit() {
        try {
            this.stateFacade.edit(obj);
            obj.setIDCountry(objC);

            set_message(FacesMessage.SEVERITY_INFO, "Edit Religion", "Edit " + obj.getState() + " successfull.");
        } catch (Exception e) {
            set_message(FacesMessage.SEVERITY_WARN, "Edit Religion", "Edit " + obj.getState() + " error !!!");
        }
    }

    public void remove(State s) {
        try {
            this.stateFacade.remove(s);
            set_list();

            set_message(FacesMessage.SEVERITY_INFO, "Remove Religion", "Remove " + obj.getState()+ " successfull.");
        } catch (Exception e) {
            set_message(FacesMessage.SEVERITY_WARN, "Remove Religion", "Remove " + obj.getState() + " error !!!");
        }
    }
    
    /**
     * ********************************************
     */
    
    public void doRedirect(Country c){
        try {
            objC = c;
            list = c.getStateList();
            
            FacesContext context = FacesContext.getCurrentInstance();
            context.getExternalContext().redirect("state.xhtml");
        } catch (Exception e) {
        }
    }
    
    public void set_list() {
        try {
            list = this.stateFacade.getState_Country(objC);
        } catch (Exception e) {
            list = null;
        }
    }

    private void set_message(FacesMessage.Severity severity, String title, String message) {
        try {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(severity, title, message));
        } catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", "Error message !!!"));
        }
    }
    
}
